package com.example.database;


import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class DataBaseAdapter {

    // Database fields
    public static final String KEY_ROWID = "_id";
    public static final String KEY_CATEGORY = "category";
    public static final String KEY_PRIORITY = "priority";
    public static final String KEY_ABOUT = "about";
    public static final String KEY_DATE = "edate";
    public static final String KEY_TIME = "etime";
    public static final String KEY_PLACE = "place";
    private static final String DATABASE_TABLE = "event_data";
    private Context context;
    private SQLiteDatabase database;
    private DataBaseHelper dbHelper;

    public DataBaseAdapter(Context context) {

        this.context = context;
    }

    //opens the database via the TodoDatabaseHelper class
    public DataBaseAdapter open() throws SQLException {

        dbHelper = new DataBaseHelper(context);
        database = dbHelper.getWritableDatabase();
        return this;
    }

    public void close() {

        dbHelper.close();
    }

    /**
         * Create a new todo If the todo is successfully created return the new
         * rowId for that note, otherwise return a -1 to indicate failure.
         */

    public long createEvent(Event item) {

        ContentValues initialValues = createContentValues(item);

        return database.insert(DATABASE_TABLE, null, initialValues);
    }

    /**
         * Update the todo
         */

    public boolean updateEvent(long rowId, Event item) {

        ContentValues updateValues = createContentValues(item);

        return database.update(DATABASE_TABLE, updateValues, KEY_ROWID + "=" + rowId, null) > 0;
    }

    /**
         * Deletes todo
         */

    public boolean deleteEvent(long rowId) {

        return database.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
    }

    /**
         * Return a Cursor over the list of all todo in the database
         * 
         * @return Cursor over all notes
         */

    public Cursor fetchAllEvents() {

        return database.query(DATABASE_TABLE, new String[] {
                KEY_ROWID, KEY_ABOUT, KEY_CATEGORY, KEY_PRIORITY, KEY_DATE, KEY_TIME, KEY_PLACE
        }, null, null, null, null, null,null);
    }

    /**
         * Return a Cursor positioned at the defined todo
         */

    public Cursor fetchTodo(long rowId) throws SQLException {

        Cursor mCursor = database.query(true, DATABASE_TABLE, new String[] {
        		KEY_ROWID, KEY_ABOUT, KEY_CATEGORY, KEY_PRIORITY, KEY_DATE, KEY_TIME, KEY_PLACE
        }, KEY_ROWID + "=" + rowId, null, null, null, null, null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;
    }

    private ContentValues createContentValues(Event items) {

        ContentValues values = new ContentValues();
        values.put(KEY_ABOUT, items.about);
        values.put(KEY_CATEGORY, items.category);
        values.put(KEY_PRIORITY, items.priority);
        values.put(KEY_DATE, items.edate);
        values.put(KEY_TIME, items.etime);
        values.put(KEY_PLACE, items.place);
        return values;
    }
}
